﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.IO;
using System.Configuration;

namespace WandaSolution.ControlTemplates.WandaSolution
{
    public partial class WandaHtmlControl : UserControl
    {
        private string htmlFileName = "footer.html";
        private string siteName = "wandavista";

        public string HtmlToBeDisplayed
        {
            get { return htmlFileName; }
            set { htmlFileName = value; }
        }

        public string HotelSiteName
        {
            get { return siteName; }
            set { siteName = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    //string wandaGroupUrl = ConfigurationManager.AppSettings["WandaSiteCollectionUrl"];                    
                    //string siteIncludeDir = siteName + "IncludeDir";
                    //string wandaSiteIncludeDir = ConfigurationManager.AppSettings[siteIncludeDir];

                    string currentURL = Request.Url.ToString();
                    if (siteName != "context")
                    {
                        string wandaSiteIncludeDir = "/PageAssets/" + siteName + "/public/include";
                        //using (SPSite sps = new SPSite(wandaGroupUrl))
                        using (SPSite sps = new SPSite(currentURL))
                        {
                            using (SPWeb spw = sps.RootWeb)
                            {
                                string folderUrl = wandaSiteIncludeDir;

                                SPFolder oFolder = spw.GetFolder(folderUrl);
                                SPFileCollection collFile = oFolder.Files;

                                SPFile spf;
                                foreach (SPFile oFile in collFile)
                                {
                                    if (oFile.Name == htmlFileName)
                                    {
                                        spf = oFile;
                                        Stream spfs = spf.OpenBinaryStream();
                                        spfs.Position = 0;
                                        using (StreamReader sr = new StreamReader(spfs))
                                        {
                                            string codeline = sr.ReadLine();
                                            while (codeline != null)
                                            {
                                                forDisplayWandaHtml.InnerHtml = forDisplayWandaHtml.InnerHtml + codeline;
                                                codeline = sr.ReadLine();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        
                    }
                    else
                    {
                        SPWeb spw = SPContext.Current.Web;                        
                        string folderUrl = spw.Url + "/PageAssets/public/include";
                        SPFolder oFolder = spw.GetFolder(folderUrl);
                        SPFileCollection collFile = oFolder.Files;

                        SPFile spf;
                        foreach (SPFile oFile in collFile)
                        {
                            if (oFile.Name == htmlFileName)
                            {
                                spf = oFile;
                                Stream spfs = spf.OpenBinaryStream();
                                spfs.Position = 0;
                                using (StreamReader sr = new StreamReader(spfs))
                                {
                                    string codeline = sr.ReadLine();
                                    while (codeline != null)
                                    {
                                        forDisplayWandaHtml.InnerHtml = forDisplayWandaHtml.InnerHtml + codeline;
                                        codeline = sr.ReadLine();
                                    }
                                }
                            }
                        } 
                    }
                }
            }
            catch (Exception ex)
            {
                forDisplayWandaHtml.InnerHtml = "Error, please check" + ex.Message;
                throw new Exception("Error occurs on usercontrol WandaHtmlUserControl" + "\r\n" + ex.Message);
            }
        }
    }
}
